﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YuuzleComic.Common;
using YuuzleComic.DBUtility;

public partial class downloadtest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            //List<string> sl = new List<string>();
            //HtmlWeb webClient = new HtmlWeb();
            //HtmlAgilityPack.HtmlWeb.PreRequestHandler handler = delegate(HttpWebRequest request)
            //{
            //    request.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
            //    request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
            //    request.CookieContainer = new System.Net.CookieContainer();
            //    request.Timeout = 500000;
            //    return true;
            //};
            //webClient.PreRequest += handler;
            //DataSet ds = DbHelperSQL.Query("select top(1000) id from comics order by id desc");
            //foreach (DataRow dr in ds.Tables[0].Rows)
            //{
            //    HtmlDocument doc = webClient.Load("http://www.baidu.com/s?tn=baiduhome_pg&ie=utf-8&bs=http%3A%2F%2Fwww.yzmanga.com%2Fcomic%2F" + dr["id"] + "%2F&f=8&rsv_bp=1&rsv_spt=1&wd=http%3A%2F%2Fwww.yzmanga.com%2Fcomic%2F" + dr["id"] + "%2F&inputT=0");
            //    if (doc.DocumentNode.SelectSingleNode("//div[@class='nors']") != null)
            //    {
            //        sl.Add(dr["id"].ToString());
            //    }
            //}
            //Input.WriteTextFile("~/nors.txt", string.Join(",", sl.ToArray()));


            //DataSet ds = DbHelperSQL.Query("select * from zz order by id");
            //foreach (DataRow dr in ds.Tables[0].Rows)
            //{
            //    HtmlWeb web = new HtmlWeb();
            //    HtmlDocument doc = web.Load("http://baike.baidu.com/search/word?word=" + dr["zzmc"] + "&pic=1&sug=1&enc=utf8");
            //    HtmlNode imgnode = doc.DocumentNode.SelectSingleNode("//img[starts-with(@class,'card-image')]");
            //    if (imgnode != null)
            //    {
            //        string filename = System.IO.Path.GetFileName(imgnode.Attributes["src"].Value);
            //        WebClient wc = new WebClient();
            //        byte[] b = wc.DownloadData(imgnode.Attributes["src"].Value);
            //        System.IO.MemoryStream ms = new System.IO.MemoryStream(Input.ResizeImageFile(b, 120, 160));
            //        System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
            //        img.Save(Server.MapPath("~/zpic/" + filename), System.Drawing.Imaging.ImageFormat.Jpeg);
            //        DbHelperSQL.ExecuteSql("update zz set zztp='" + filename + "' where id=" + dr["id"]);
            //    }
            //    HtmlNode paranode = doc.DocumentNode.SelectSingleNode("//div[@class='card-summary-content']/div[@class='para']");
            //    if (paranode != null && dr["zzjs"].ToString().Trim() == "<h2 class=\"blue\">" + dr["zzmc"] + "漫画全集</h2>")
            //    {
            //        SqlParameter[] parameter = new SqlParameter[] { 
            //            new SqlParameter("@zzjs",SqlDbType.Text)
            //        };
            //        parameter[0].Value = paranode.InnerText;
            //        DbHelperSQL.ExecuteSql("update zz set zzjs=@zzjs where id=" + dr["id"], parameter);
            //    }
            //}


            Process prc = new Process();
            prc.StartInfo.FileName = "cmd.exe";
            prc.StartInfo.UseShellExecute = false;
            prc.StartInfo.RedirectStandardInput = true;
            prc.StartInfo.RedirectStandardOutput = true;
            prc.StartInfo.RedirectStandardError = true;
            prc.StartInfo.CreateNoWindow = false;
            //prc.StartInfo.UserName = "administrator"; //指定用户  
            ////构造用户密码，假定密码为123，必须一个字符一个字符的添加  
            //System.Security.SecureString password = new System.Security.SecureString();
            //password.AppendChar('s');
            //password.AppendChar('e');
            //password.AppendChar('o');
            //password.AppendChar('1');
            //password.AppendChar('2');
            //password.AppendChar('1');
            //password.AppendChar('4');
            //prc.StartInfo.Password = password;

            prc.Start();
            prc.StandardInput.WriteLine(@"tesseract " + Server.MapPath("~/upload/validate.jpg") + " " + Server.MapPath("~/upload/validate") + " digits");
            prc.StandardInput.Close();
            Response.Write(prc.StandardOutput.ReadToEnd());

          string s = "PGltZyBpZD0iY3VyX3BpYyIgc3R5bGU9InBvc2l0aW9uOmFic29sdXRlO3RvcDowO2xlZnQ6MCIgc3JjPSJodHRwOi8vaW1nNS5jZG4udTE3aS5jb20vMTAvMDkvNzA2NS8xMDgwMDVfMTI4MzQzNTg2NV9tT1dkWmRNb25CQ2MuNDgwZGNfc3ZvbC5qcGciIHdpZHRoPSI5MDAiIGhlaWdodD0iMTI2NSIgb25sb2FkPSJibG9ja19waWMoKSIgYm9yZGVyPSIwIi8+";
          //Response.Write(Input.DecodeFrom64(s));
            //string s1 = "http://lt.jmydm.jmmh.net:2011/jmydm2/40296/223019/jmydm0001-19435.JPG";
            //Response.Write(Input.EncodeTo64(s));
            string imgUrl = "http://58.218.39.221:82/comic/F/%E8%BF%94%E9%AD%82%E5%B0%91%E5%A5%B3/%E7%AC%AC15%E8%AF%9D%E9%A2%84%E5%91%8A/1.jpg";
            HttpWebRequest myHttpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(imgUrl);//moviePath为图片的真实URL  
            string host = new Uri(imgUrl).Host;
            //myHttpWebRequest.Referer = "http://www.jmydm.com/";//伪造网址
            myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0;)";
            myHttpWebRequest.CookieContainer = new CookieContainer(); //暂存到新实例
            System.Net.HttpWebResponse response = myHttpWebRequest.GetResponse() as System.Net.HttpWebResponse;
            System.IO.Stream responseStream = response.GetResponseStream();
            System.Drawing.Image img = System.Drawing.Image.FromStream(responseStream);
            img.Save("F:/1.jpg");//保存到硬盘
            responseStream.Close();
            response.Close();
        }
    }

    protected void gethua()
    {
        Random random = new Random();
        Bitmap bt = new Bitmap(351, 402, PixelFormat.Format24bppRgb);
        Graphics g = Graphics.FromImage(bt);
        Font fn1 = new Font("微软雅黑", 10, FontStyle.Bold);
        Font fn = new Font("微软雅黑", 9, FontStyle.Bold);
        g.Clear(Color.Honeydew);
        g.DrawString("中国电子学会电子设计初级工程师认证考试名", fn1, Brushes.Black, new PointF(20, 20));
        g.DrawString("准 考 证", fn1, Brushes.Black, new PointF(140, 50));
        g.DrawString("准考证号", fn, Brushes.Black, new PointF(14, 85));
        g.DrawString("CIE09101080004", fn, Brushes.Black, new PointF(90, 85));
        g.DrawString("考生姓名", fn, Brushes.Black, new PointF(14, 115));
        g.DrawString("李晓杰", fn, Brushes.Black, new PointF(90, 115));
        g.DrawString("考生性别", fn, Brushes.Black, new PointF(14, 145));
        g.DrawString("男", fn, Brushes.Black, new PointF(90, 145));
        g.DrawString("身份证号", fn, Brushes.Black, new PointF(14, 175));
        g.DrawString("410381198408272032", fn, Brushes.Black, new PointF(90, 175));
        g.DrawString("考试时间", fn, Brushes.Black, new PointF(14, 205));
        g.DrawString("2007年11月30号", fn, Brushes.Black, new PointF(90, 205));
        g.DrawString("考试科目", fn, Brushes.Black, new PointF(14, 235));
        g.DrawString("电子设计", fn, Brushes.Black, new PointF(90, 235));
        g.DrawString("考点名称", fn, Brushes.Black, new PointF(14, 265));
        g.DrawString("电子设计工程师证书", fn, Brushes.Black, new PointF(90, 265));
        g.DrawString("考场编号", fn, Brushes.Black, new PointF(14, 295));
        g.DrawString("9101080004", fn, Brushes.Black, new PointF(90, 295));
        g.DrawString("机位编号", fn, Brushes.Black, new PointF(196, 295));
        g.DrawString("080004", fn, Brushes.Black, new PointF(275, 295));
        g.DrawString("交通指南(由各考点提供)", fn, Brushes.Black, new PointF(14, 325));
        string dizhi = "";
        int j = 320;    //初始坐标
        for (int z = 0; z < 61; z += 23)  //循环次数 
        {
            int k = z + 20;  // 第一次为0时 
            if (k > dizhi.Length)  //k是否大于总字数
            {
                k = dizhi.Length - k;  // 若大于比如k=45  dizhi.length=40  k=40-45    k=-5
                int i = k + 20;      //i = -5+20  这样就不至于截取超出范围
                g.DrawString(dizhi.Substring(z, i), fn, Brushes.Black, new PointF(14, j += 18));
                break;
            }
            g.DrawString(dizhi.Substring(z, 23), fn, Brushes.Black, new PointF(14, j += 18));
        }
        //产生杂点
        for (int i = 0; i < 500; i++)
        {
            int x1 = random.Next(bt.Width - 20);
            int y1 = random.Next(bt.Height - 20);
            bt.SetPixel(x1, y1, Color.FromArgb(random.Next()));
        }
        //产生随机曲线
        for (int i = 0; i < 50; i++)
        {
            int x1 = random.Next(bt.Width - 20);
            int y1 = random.Next(bt.Height - 20);
            int x2 = random.Next(1, 30);
            int y2 = random.Next(1, 20);
            int x3 = random.Next(15, 45);
            int y3 = random.Next(70, 270);
            g.DrawArc(new Pen(Color.FromArgb(random.Next())), x1, y1, x2, y2, x3, y3);
        }
        //画直线
        g.DrawLine(new Pen(Color.Black), 10, 75, 341, 75);
        g.DrawLine(new Pen(Color.Black), 10, 105, 243, 105);
        g.DrawLine(new Pen(Color.Black), 10, 135, 243, 135);
        g.DrawLine(new Pen(Color.Black), 10, 165, 243, 165);
        g.DrawLine(new Pen(Color.Black), 10, 195, 341, 195);
        g.DrawLine(new Pen(Color.Black), 10, 225, 341, 225);
        g.DrawLine(new Pen(Color.Black), 10, 255, 341, 255);
        g.DrawLine(new Pen(Color.Black), 10, 285, 341, 285);
        g.DrawLine(new Pen(Color.Black), 10, 315, 341, 315);
        g.DrawLine(new Pen(Color.Black), 80, 75, 80, 314);
        g.DrawLine(new Pen(Color.Black), 185, 285, 185, 314);
        g.DrawLine(new Pen(Color.Black), 265, 285, 265, 314);
        g.DrawLine(new Pen(Color.Black), 243, 75, 243, 195);
        //画方框
        g.DrawRectangle(new Pen(Color.Black), 10, 10, 331, 382);
        //填充图像
        System.Drawing.Image newimage = System.Drawing.Image.FromFile(Server.MapPath("~/cpic/160.gif"));
        //图像定位
        g.DrawImage(newimage, 245, 77, 95, 117);
        //释放图像缓存
        g.Dispose();
        //定义产生图片格式
        Response.ContentType = "image/Jpeg";
        Response.Clear();
        bt.Save(Response.OutputStream, ImageFormat.Jpeg);
        bt.Dispose();
        Response.End();
        
    }   
}